import 'package:cbec_app/components/base/base_chekbox.dart';
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/goods/goods_add_minus.dart';
import 'package:cbec_app/pages/my/controller/after_pop_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class AfterGoodsItem extends StatelessWidget {
  final bool? showCheckBox;
  final bool? showAdd;
  final bool? hasMargin;

  ///商品
  final Map? item;

  final AfterPopController afterPopCtr = Get.put(AfterPopController());

  AfterGoodsItem(
      {super.key,
      this.showCheckBox = true,
      this.showAdd = true,
      this.hasMargin = true,
      this.item});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: hasMargin == true
          ? EdgeInsets.only(bottom: 20.h)
          : EdgeInsets.all(0.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: [
          Visibility(
            visible: showCheckBox!,
            child: Container(
              margin: EdgeInsets.only(right: 10.w),
              height: 80.h,
              alignment: Alignment.centerLeft,
              child: BaseCheckBox(
                value: false,
                onChange: (selected) {
                  afterPopCtr.selectGoods(selected, item!);
                },
              ),
            ),
          ),
          Container(
            child: BaseImage(
                url: item?['img'] ?? '',
                width: 100,
                height: 100,
                borderRadius: BorderRadius.circular(20)),
          ),
          SizedBox(width: 18.w),
          Expanded(
            child: Container(
              color: Colors.white,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 8.h),
                  Container(
                    padding: const EdgeInsets.only(right: 10.0),
              
                    // width: 200.w,
                    child: Text(
                      item?['goodsName'] ?? '',
                      style: setTextStyle(
                          14, FontWeight.w300, Tools.fromHex("#333333")),
                      maxLines: 3,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  SizedBox(height: 8.h),
                  SizedBox(
                    width: 180.w,
                    child: Text(
                      item?['spuSpecBody'] != null ? (item?['spuSpecBody']) : '', //
                      style: setTextStyle(
                          12, FontWeight.normal, Tools.fromHex("#666666")),
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  SizedBox(height: 8.h),
                  Container(
                    color: Colors.white,
                    // width: 200.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Text("￥${item?['price']}",
                            overflow: TextOverflow.ellipsis,
                            style: setTextStyle(
                                14, FontWeight.w300, Tools.fromHex("#333333"))),
                        
                        showAdd == true
                            ? Obx(() => GoodsAddMinus(
                                  num: afterPopCtr.num.value,
                                  max: item?['buyNum'],
                                  onAdd: (nums) =>
                                      afterPopCtr.setNum(item!['goodsOrderId'], nums),
                                  onMinus: (nums) =>
                                      afterPopCtr.setNum(item!['goodsOrderId'], nums),
                                ))
                            : SizedBox()
                      ],
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

class AfterGoodsItem2 extends StatefulWidget {
  ///商品
  final Map? item;

  final AfterPopController afterPopCtr = Get.put(AfterPopController());

  AfterGoodsItem2({super.key, this.item});

  @override
  State<AfterGoodsItem2> createState() => _AfterGoodsItem2State();
}

class _AfterGoodsItem2State extends State<AfterGoodsItem2> {
  @override
  void initState() {
    super.initState();
    widget.afterPopCtr.selectGoods(true, widget.item!);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        BaseImage(
            url: widget.item?['img'] ?? '',
            width: 100,
            height: 100,
            borderRadius: BorderRadius.circular(20)),
        SizedBox(width: 18.w),
        Expanded(
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 10.0),
            height: 100,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  widget.item?['goodsName'] ?? '',
                  style: setTextStyle(
                      14, FontWeight.w300, Tools.fromHex("#333333")),
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                ),
                Text(
                  widget.item?['spuSpecBody'] != null
                      ? (widget.item?['spuSpecBody'])
                      : '', //
                  style: setTextStyle(
                      12, FontWeight.normal, Tools.fromHex("#666666")),
                  overflow: TextOverflow.ellipsis,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Text("￥${widget.item?['price']}",
                        style: setTextStyle(
                            14, FontWeight.w300, Tools.fromHex("#333333"))),
                    Text("数量：${widget.item?['buyNum']}",
                        style: setTextStyle(
                            14, FontWeight.w300, Tools.fromHex("#333333"))),
                  ],
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
